﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using PlayDeezGames.Common;
using System.Xml.Linq;
using System.Collections.Generic;

namespace DungeonDelver.Engine
{
    public class ConfigurationTable:DescriptoredEntityBase
    {
        public class Factory : IEntityFactory
        {
            public IEntity LoadFromXElement(IEntity theParent, XElement theXElement)
            {
                return new ConfigurationTable(theParent as Configuration, theXElement);
            }

            public IEntity Create(IEntity theParent)
            {
                return new ConfigurationTable(theParent as Configuration);
            }

            private Factory() { }

            public static void Register()
            {
                EntityFactories.RegisterFactory(Names.EngineFactories.ConfigurationTableFactory, new ConfigurationTable.Factory());
            }
            public static void Unregister()
            {
                EntityFactories.UnregisterFactory(Names.EngineFactories.ConfigurationTableFactory);
            }

        }
        public string Identifier
        {
            get
            {
                foreach(var table in Configuration.Tables)
                {
                    if(Configuration[table]==this)
                    {
                        return table;
                    }
                }
                throw new IndexOutOfRangeException();
            }
        }
        public Configuration Configuration
        {
            get
            {
                return ParentEntity as Configuration;
            }
        }
        public Game Game
        {
            get
            {
                return Configuration!=null?Configuration.Game:null;
            }
        }

        private ConfigurationTable(Configuration parent)
            : base(parent)
        {
        }

        private ConfigurationTable(Configuration parent, XElement theXElement)
            : base(parent, theXElement)
        {
        }

        private Dictionary<string, IdentifiedDescriptor> identifiedDescriptors = new Dictionary<string, IdentifiedDescriptor>();

        public Dictionary<string, IdentifiedDescriptor>.KeyCollection Identifiers
        {
            get
            {
                return identifiedDescriptors.Keys;
            }
        }

        public IdentifiedDescriptor this[string index]
        {
            get
            {
                if (identifiedDescriptors.ContainsKey(index))
                {
                    return identifiedDescriptors[index];
                }
                else
                {
                    return null;
                }
            }
            set
            {
                if (identifiedDescriptors.ContainsKey(index))
                {
                    identifiedDescriptors[index].ParentEntity = null;
                    identifiedDescriptors.Remove(index);
                }
                if (value != null)
                {
                    identifiedDescriptors[index] = value;
                }
            }
        }
        protected override System.Xml.Linq.XElement OnToXElement()
        {
            XElement result = new XElement(Names.EngineXNames.ConfigurationTable);
            result.Add(new XAttribute(PlayDeezGames.Common.Names.CommonXNames.Factory, Names.EngineFactories.ConfigurationTableFactory));
            result.Add(new XAttribute(PlayDeezGames.Common.Names.CommonXNames.Identifier, Identifier));
            return result;
        }

        protected override void OnFromXElement(System.Xml.Linq.XElement theXElement)
        {
            base.OnFromXElement(theXElement);
            if (Configuration != null)
            {
                Configuration[theXElement.Attribute(PlayDeezGames.Common.Names.CommonXNames.Identifier).Value] = this;
            }
        }

        public static ConfigurationTable Create(Configuration theParent)
        {
            return EntityFactories.Create(theParent, Names.EngineFactories.ConfigurationTableFactory) as ConfigurationTable;
        }
        public static ConfigurationTable Load(Configuration theParent, XElement theXElement)
        {
            return EntityFactories.Load(theParent, theXElement) as ConfigurationTable;
        }
    }
}
